using System;
using System.ComponentModel;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using MEA.AE_BusinessLogic;
using MEA.AE_Data.Config;

namespace E4W
{
	public class UIProgressbar : Form
	{
		private IContainer components;

		private Panel panel1;

		private Label label2;

		private Label label1;

		public ProgressBar progressBar1;

		private Button b_Cancel;

		private System.Threading.Timer StepTimer;

		private string originalText;

		private Label label3;

		private Label label4;

		public event EventHandler b_CancelPressed;

		protected override void Dispose(bool disposing)
		{
			if (disposing && components != null)
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		private void InitializeComponent()
		{
			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(E4W.UIProgressbar));
			this.panel1 = new System.Windows.Forms.Panel();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.progressBar1 = new System.Windows.Forms.ProgressBar();
			this.b_Cancel = new System.Windows.Forms.Button();
			this.panel1.SuspendLayout();
			base.SuspendLayout();
			this.panel1.BackColor = System.Drawing.Color.White;
			this.panel1.Controls.Add(this.label2);
			this.panel1.Controls.Add(this.label1);
			this.panel1.Controls.Add(this.progressBar1);
			this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
			this.panel1.Location = new System.Drawing.Point(0, 0);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(340, 140);
			this.panel1.TabIndex = 0;
			this.label2.AutoSize = true;
			this.label2.Font = new System.Drawing.Font("Tahoma", 8.25f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 0);
			this.label2.Location = new System.Drawing.Point(9, 32);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(163, 13);
			this.label2.TabIndex = 2;
			this.label2.Text = "This might take several seconds.";
			this.label1.AutoSize = true;
			this.label1.Font = new System.Drawing.Font("Tahoma", 10f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 0);
			this.label1.ForeColor = System.Drawing.SystemColors.Highlight;
			this.label1.Location = new System.Drawing.Point(9, 9);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(304, 17);
			this.label1.TabIndex = 1;
			this.label1.Text = "Please wait while operations are performed";
			this.progressBar1.Location = new System.Drawing.Point(12, 115);
			this.progressBar1.Name = "progressBar1";
			this.progressBar1.Size = new System.Drawing.Size(316, 13);
			this.progressBar1.Style = System.Windows.Forms.ProgressBarStyle.Continuous;
			this.progressBar1.TabIndex = 0;
			this.b_Cancel.Location = new System.Drawing.Point(253, 146);
			this.b_Cancel.Name = "b_Cancel";
			this.b_Cancel.Size = new System.Drawing.Size(75, 23);
			this.b_Cancel.TabIndex = 1;
			this.b_Cancel.Text = "Cancel";
			this.b_Cancel.UseVisualStyleBackColor = true;
			this.b_Cancel.Click += new System.EventHandler(b_Cancel_Click);
			base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
			base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.AutoSize = true;
			base.ClientSize = new System.Drawing.Size(340, 173);
			base.ControlBox = false;
			base.Controls.Add(this.b_Cancel);
			base.Controls.Add(this.panel1);
			base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			base.Icon = (System.Drawing.Icon)resources.GetObject("$this.Icon");
			base.Name = "UIProgressbar";
			base.ShowIcon = false;
			this.Text = "AE 300 -Wizard";
			this.panel1.ResumeLayout(false);
			this.panel1.PerformLayout();
			base.ResumeLayout(false);
		}

		public UIProgressbar(object text, int maxValue)
		{
			InitializeComponent();
			progressBar1.Minimum = 0;
			progressBar1.Maximum = maxValue;
			progressBar1.Step = 1;
			progressBar1.Value = progressBar1.Minimum;
			if (text is string)
			{
				label2.Text = (string)text;
				originalText = (string)text;
			}
			else if (text is ConfigData.SUIProgressbar)
			{
				ConfigData.SUIProgressbar sUIProgressbar = (ConfigData.SUIProgressbar)text;
				progressBar1.Maximum = sUIProgressbar.val;
				int num = 9;
				label3 = new Label();
				label3.AutoSize = true;
				label3.Font = new Font("Tahoma", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
				label3.Location = new Point(num, 96);
				label3.Name = "label3";
				label3.Size = new Size(128, 12);
				num += label3.Size.Width + 32;
				label3.Text = "ECUA";
				panel1.Controls.Add(label3);
				label3.Enabled = true;
				label3.Visible = true;
				label4 = new Label();
				label4.AutoSize = true;
				label4.Font = new Font("Tahoma", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
				label4.Location = new Point(num, 96);
				label4.Name = "label3";
				label4.Size = new Size(128, 12);
				label4.Text = "ECUB";
				panel1.Controls.Add(label4);
				label4.Enabled = true;
				label4.Visible = true;
				label2.Text = sUIProgressbar.msg;
				originalText = sUIProgressbar.msg;
			}
			progressBar1.Style = ProgressBarStyle.Marquee;
			CenterToParent();
		}

		public UIProgressbar(string text, bool cancelButton)
		{
			InitializeComponent();
			originalText = text;
			label2.Text = text;
			progressBar1.Style = ProgressBarStyle.Continuous;
			b_Cancel.Visible = cancelButton;
			CenterToParent();
		}

		public void TextChange(string text)
		{
			label2.Text = text;
		}

		public void PerformStep(object stepInfo)
		{
			try
			{
				Invoke((MethodInvoker)delegate
				{
					ServiceLayerStringEventArg serviceLayerStringEventArg = (ServiceLayerStringEventArg)stepInfo;
					if (label3 == null || label4 == null)
					{
						label2.Text = string.Format("{0}\r\n\n{1}", originalText, serviceLayerStringEventArg.Message);
					}
					else if (serviceLayerStringEventArg.SuiProgressbar.ecuId == ConfigData.e_ECUID.A)
					{
						label3.Text = serviceLayerStringEventArg.SuiProgressbar.msg;
					}
					else
					{
						label4.Text = serviceLayerStringEventArg.SuiProgressbar.msg;
					}
					if (progressBar1.Value < progressBar1.Maximum)
					{
						progressBar1.Value++;
					}
				});
			}
			catch
			{
			}
		}

		public void StartTimer(int gesatmdauer, int interval)
		{
			progressBar1.Minimum = 0;
			progressBar1.Maximum = gesatmdauer;
			Console.WriteLine("Start Timer: progressBar1.Maximum = {0}", progressBar1.Maximum);
			progressBar1.Style = ProgressBarStyle.Continuous;
			progressBar1.Step = interval;
			StepTimer = new System.Threading.Timer(TimerControlledStep, null, 0, interval);
		}

		private void TimerControlledStep(object o)
		{
			try
			{
				Invoke((MethodInvoker)delegate
				{
					if (progressBar1.Value < progressBar1.Maximum)
					{
						progressBar1.PerformStep();
					}
					else
					{
						CloseTimer();
					}
				});
			}
			catch
			{
			}
		}

		public void CloseTimer()
		{
			try
			{
				if (StepTimer != null)
				{
					StepTimer.Dispose();
				}
				Close();
			}
			catch
			{
			}
		}

		public void b_Cancel_Click(object sender, EventArgs e)
		{
			if (StepTimer != null)
			{
				StepTimer.Dispose();
			}
			if (this.b_CancelPressed != null)
			{
				this.b_CancelPressed(this, null);
			}
			Close();
		}
	}
}
